package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Comment;

import org.apache.log4j.Logger;

import common.Constant;

import util.KeyGen;
import util.MyJson;
import util.ParameterCheck;
import dao.CommentDao;
import dao.ItemDao;

/**
 * Servlet implementation class AddComment
 */
public class AddComment extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(AddComment.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddComment() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");

		String userid = "";
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			if ("user".equals(cookies[i].getName())) {
				userid = cookies[i].getValue();
			}
		}

		String iid = request.getParameter("itemid");
		String content = request.getParameter("content");

		String msg = "";

		if (ParameterCheck.isEmpty(userid)) {
			msg =Constant.nologin ;//用户未登录
		} else if (ParameterCheck.isEmpty(iid)) {
			msg = Constant.syserror;//系统错误 
		} else if (ParameterCheck.isEmpty(content)) {
			msg = Constant.invalid;//评论内容为空
		} else {

			userid = KeyGen.getId(userid);
			//			iid = KeyGen.getId(iid);
			int uid = Integer.valueOf(userid);
			int itemid = KeyGen.convertCode2Id(iid);

			if (checkCommentTime(uid,itemid) >= 3) {
				msg =Constant.toomanytimes;//对同一用户评论多余3条
			} else if (2000 < content.length()) {
				msg =Constant.toomanywords;//评论超过2000字
			} else {
				Comment comment = new Comment();
				comment.setContent(content);
				comment.setItemid(itemid);
				comment.setUid(uid);
				msg = "ok";
				msg=String.valueOf(new CommentDao().CreateComment(comment));
				new ItemDao().updateCommentCount(itemid, 1);//增加评论数
//				log.info(new ItemDao().updateHot(itemid, 5));

			}
		}

//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("msg", msg);
//		jsonObject.put("res", result);
//		out.print(jsonObject.toString());
		out.print(MyJson.toJson("result", msg));

		log.info(msg);

		out.flush();
		out.close();
	}

	private int checkCommentTime(int uid,int itemid) {
		return new CommentDao().getCommentTime(uid,itemid);
	}
}
